import request from '@/config/axios'
import type { Dayjs } from 'dayjs'

/** 目录信息 */
export interface OxDirectory {
  id: number // 训练目录id
  directoryName: string // 目录名
  projectId: number // 项目id
  imagesNum: number // 图片数量
}

// 目录 API
export const OxDirectoryApi = {
  // 查询目录分页
  getOxDirectoryPage: async (params: any) => {
    return await request.get({ url: `/happy-ox-core/ox-directory/page`, params })
  },

  // 查询目录详情
  getOxDirectory: async (id: number) => {
    return await request.get({ url: `/happy-ox-core/ox-directory/get?id=` + id })
  },

  // 新增目录
  createOxDirectory: async (data: OxDirectory) => {
    return await request.post({ url: `/happy-ox-core/ox-directory/create`, data })
  },

  // 修改目录
  updateOxDirectory: async (data: OxDirectory) => {
    return await request.put({ url: `/happy-ox-core/ox-directory/update`, data })
  },

  // 删除目录
  deleteOxDirectory: async (id: number) => {
    return await request.delete({ url: `/happy-ox-core/ox-directory/delete?id=` + id })
  },

  /** 批量删除目录 */
  deleteOxDirectoryList: async (ids: number[]) => {
    return await request.delete({
      url: `/happy-ox-core/ox-directory/delete-list?ids=${ids.join(',')}`
    })
  },

  // 导出目录 Excel
  exportOxDirectory: async (params) => {
    return await request.download({ url: `/happy-ox-core/ox-directory/export-excel`, params })
  },
  selectByProjectId: async (id: any) => {
    return await request.post({
      url: `/happy-ox-core/ox-directory/getDirectoryByProjectId?projectId=` + id
    })
  },
  getDirectoryPath: async (id: any) => {
    return await request.post({
      url: `/happy-ox-core/ox-directory/getDirectoryPath?id=` + id
    })
  },
  addType: async (params: any) => {
    return await request.post({
      url:
        `/happy-ox-core/ox-directory/addType?projectId=` + params.projectId + '&name=' + params.name
    })
  },
  // 文件：@/api/happycore/ox-directory.ts （或你当前的文件）

  // 新增：上传文件到指定目录
  uploadFilesToDirectory: async (directoryId: number, files: File[]) => {
    const formData = new FormData()
    formData.append('directoryId', directoryId.toString())
    files.forEach((file) => {
      formData.append('files', file)
    })

    // 注意：这里使用 request.post，但传的是 formData，且不设 data，而是作为请求体
    // 如果你的 request 封装支持自动识别 FormData，则直接传
    return await request.post({
      url: '/upload', // 注意：你的后端是 /upload，不是 /happy-ox-core/upload
      data: formData,
      headers: {
        'Content-Type': 'multipart/form-data'
      }
    })
  }
}
